package com.pan.note.im.netty;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@ChannelHandler.Sharable
@Slf4j
public class HeatBeatHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent){
            //如果接收到的事件消息属于我们之前定义的心跳事件
            IdleStateEvent idleStateEvent=(IdleStateEvent) evt;
            //将该事件消息强转为心跳事件
            if (idleStateEvent.state()== IdleState.ALL_IDLE){
                log.info("关闭空闲通道");
                ctx.channel().close();//关闭该通道
            }
        }

    }
}
